package com.alittle.core;

import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.LoaderManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

import com.alittle.R;
import com.alittle.core.view.PseudoToolbar;
import com.alittle.core.view.V4FrameActionMenu;

import dev.xesam.androidkit.utils.ViewUtil;

/**
 * Created by wom on 2015/8/18.
 * <br/>
 * 子类可获得Context，NotificationManager，FragmentManager，LoaderManager，SelfActionBar
 * <br/>
 * 以及RootView,Application,Activity
 * <br/>
 * 对ToolBar的支持
 */

public class ALBaseActivity extends AppCompatActivity {

    protected Context getSelfContext() {
        return this;
    }

    protected NotificationManager getSelfNotificationManager() {
        return (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    /**
     * 所有继承他的子类都可以直接引用得到 FragmentManager
     *
     * @return FragmentManager 实例
     */
    protected FragmentManager getSelfFragmentManager() {
        return getSupportFragmentManager();
    }

    protected LoaderManager getSelfLoaderManager() {
        return getSupportLoaderManager();
    }

    protected ActionBar getSelfActionBar() {
        return getSupportActionBar();
    }

    /**
     * 获取系统默认的content view(android.R.id.content)
     */
    protected View getActivityRootView() {
        return getWindow().getDecorView().findViewById(android.R.id.content);
    }

    @Deprecated
    public LittleApp getSelfApplication() {
        return (LittleApp) getApplication();
    }

    protected ALBaseActivity getSelfActivity() {
        return this;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////

    private Toolbar vToolbar;

    @Override
    public void setContentView(int id) {
        super.setContentView(id);
        fullScreen();
        initToolbar();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        fullScreen();
        initToolbar();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        fullScreen();
        initToolbar();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        setupToolbarActionMenu(getToolbarActionMenus());
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 重新构建菜单
     */
    protected void selfInvalidateOptionsMenu() {
        supportInvalidateOptionsMenu();
    }

    /**
     * 是否显示返回按钮
     */
    protected boolean isDisplayHomeAsUpEnabled() {
        return true;
    }

    /**
     * 变更返回按钮的显示与否
     */
    protected void enableToolbarHomeAsUp(boolean enable) {
        if (vToolbar == null) {
            return;
        }

        getSupportActionBar().setDisplayHomeAsUpEnabled(enable);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.un_home_back_ic);

        if (enable) {
            vToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        } else {
            vToolbar.setNavigationOnClickListener(null);
        }
    }

    public void setSelfTitle(String title) {
        getSupportActionBar().setTitle(title);
    }

    /**
     * Toolbar右上角从右到左依次为0,1,2
     */
    protected V4FrameActionMenu[] getToolbarActionMenus() {
        return null;
    }

    /**
     * 构建伪菜单显示
     */
    void setupToolbarActionMenu(V4FrameActionMenu[] items) {
        if (vToolbar instanceof PseudoToolbar) {
            ((PseudoToolbar) vToolbar).setupToolbarActionMenu(items);
        }
    }

    /**
     * 初始化Toolbar
     */
    private void initToolbar() {
        vToolbar = ViewUtil.findById(this, R.id.frame_toolbar);
        if (vToolbar != null) {
            setSupportActionBar(vToolbar);
            enableToolbarHomeAsUp(isDisplayHomeAsUpEnabled());
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////

    /**
     * 保持全屏
     * Todo 还需要进一步研究全屏沉浸模式
     */
    protected void fullScreen() {

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);}
    }

}
